<!-- 异常操作界面2 死种 死仔页面 -->
<template>
	<view>
		<scroll-view scroll-y="true" class="circle">
			<view class="ul" :class="cageCount > 6 ? 'ul-8' : 'ul-6'">
				<view class="li" v-for="item in cageCount" :key="item">
					<view class="left">
						<span class="cageId">{{item}}</span>
						<!-- 显示同步的鸽笼数量 -->
						<view class="cage-numbers">
							<span v-if="numListLength ===2" style="color: #206dc0;">
								种:{{ getCageNumber(item, 'number') }}
							</span>
							<span v-if="numListLength ===4" style="color: #206dc0;">
								仔:{{ getCageNumber(item, 'cub_number') }}
							</span>
						</view>
					</view>
					<view class="right">
						<button :class="numListLength === 2 ? 'num1' : 'num'" 
							v-for="item_child in numList" :key="item_child"
							@click="chooseFn(numList,item,item_child)">{{item_child}}</button>
					</view>
				</view>
			</view>
		</scroll-view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		reqDeath
	} from '@/api/index.js'
	import {
		getNowDate
	} from '@/getTime.js'
	import {
		mapGetters
	} from 'vuex'

	export default {
		props: {
			numList: {
				type: Array,
				required: false
			},
			cageNum: {
				type: Array,
				default: () => []
			},
			cageCount: {
				type: Number,
				default: 6
			}
		},
		data() {
			return {
				baseFormData: {
					siteId: uni.getStorageSync('siteId'),
					houseId: '',
					cageId: '',
					refer: '死亡',
					oldNum: 0,
					youngNum: 0,
					deathTime: null,
				},
				show: false,
				content: '确认操作吗？',
			};
		},
		computed: {
			...mapGetters(['nfcId', 'show_nfcId', 'cageStatu', 'loading', 'whipCount', 'stayCount']),
			numListLength() {
				return this.numList.length;
			}
		},
		methods: {
			// 获取鸽笼数量
			getCageNumber(cageIndex, type) {
				if (this.cageNum && this.cageNum[cageIndex - 1]) {
					return this.cageNum[cageIndex - 1][type] || '0';
				}
				return '0';
			},
			
			handleClick1(cageId, oldNum) {
				this.baseFormData.siteId = uni.getStorageSync('siteId');
				this.baseFormData.houseId = this.$store.state.NFC.nfcId;
				this.baseFormData.cageId = cageId;
				this.baseFormData.oldNum = oldNum;
				this.baseFormData.deathTime = getNowDate();
				reqDeath(this.baseFormData).then(data => {
					if (data.code == 200) {
						this.$refs.uToast.show({
							title: data.msg,
							type: 'success',
							duration: '600',
							position: 'top',
						})
					} else if (data.code === 404) {
						this.$refs.uToast.show({
							title: data.msg,
							type: 'warning',
							duration: '800',
							position: 'top'
						})
					}
				}).catch((e) => {
					this.$refs.uToast.show({
						title: '网络连接失败',
						type: 'error',
						duration: '800',
						position: 'top'
					})
				}).finally(() => {
					this.show = false
				})
			},
			
			handleClick2(cageId, youngNum) {
				this.baseFormData.siteId = uni.getStorageSync('siteId');
				this.baseFormData.houseId = this.$store.state.NFC.nfcId;
				this.baseFormData.cageId = cageId;
				this.baseFormData.youngNum = youngNum;
				this.baseFormData.deathTime = getNowDate();
				reqDeath(this.baseFormData).then(data => {
					if (data.code == 200) {
						this.$refs.uToast.show({
							title: data.msg,
							type: 'success',
							duration: '600',
							position: 'top',
						})
					} else if (data.code === 404) {
						this.$refs.uToast.show({
							title: data.msg,
							type: 'warning',
							duration: '800',
							position: 'top'
						})
					}
				}).catch(() => {
					this.$refs.uToast.show({
						title: '网络连接失败',
						type: 'error',
						duration: '800',
						position: 'top'
					})
				}).finally(() => {
					this.show = false
				})
			},
			
			chooseFn(numList, cage_id, deathnum) {
				if (numList.length == 2) {
					this.handleClick1(cage_id, deathnum)
				} else {
					this.handleClick2(cage_id, deathnum)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import 'styles/builder.scss';

	.circle {
		width: 100%;
		height: 80vh;

		h1 {
			text-align: center;
			font-weight: 700;
		}

		.ul-6 {
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			align-content: space-around;
			width: 100%;
			height: 80%;
			flex-wrap: wrap;
			margin-top: 20rpx;

			.li {
				display: flex;
				justify-content: space-around;
				width: 40%;
				height: 33%;
				text-align: center;
				align-items: center;
				border-radius: 20rpx;
				background-color: #dbe2f1;
				padding-bottom: 5rpx;

				.left,
				.right {
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					height: 90%;
					font-weight: bold;
				}

				.left {
					width: 35%;
					justify-content: center;
					border-right: 2px solid #7c9bd2;
					box-sizing: border-box;
					color: #4472c4;

					.cageId {
						font-size: 100rpx
					}

					.cage-numbers {
						display: flex;
						flex-direction: column;
						align-items: center;
						font-size: 24rpx;
						margin-top: 10rpx;
					}
				}

				.right {
					width: 100%;
					display: flex;
					flex-direction: row;
					justify-content: space-around;
					align-content: space-around;
					flex-wrap: wrap;

					.num1 {
						@include flex(column, center, 'items') width: 70%;
						height: 30%;
						font-size: 41.16rpx;
						font-weight: bold;
						border-radius: 10rpx;
						box-sizing: border-box;
						border: 1px solid #7c9bd2;
						justify-content: space-around;
						padding: 10rpx;
						margin: 0;
					}

					.num {
						@include flex(column, center, 'items') width: 42%;
						height: 30%;
						margin: 5rpx;
						font-size: 41.16rpx;
						font-weight: bold;
						border-radius: 10rpx;
						box-sizing: border-box;
						border: 1px solid #7c9bd2;
						justify-content: space-around;
					}
				}
			}
		}

		.ul-8 {
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			align-content: space-around;
			width: 100%;
			height: 90%;
			flex-wrap: wrap;
			margin-top: 15rpx;

			.li {
				display: flex;
				justify-content: space-around;
				width: 45%;
				height: 22%;
				text-align: center;
				align-items: center;
				border-radius: 15rpx;
				background-color: #dbe2f1;
				padding-bottom: 5rpx;
				margin: 5rpx auto;

				.left,
				.right {
					display: flex;
					flex-direction: column;
					justify-content: space-around;
					height: 90%;
					font-weight: bold;
				}

				.left {
					width: 35%;
					justify-content: center;
					border-right: 2px solid #7c9bd2;
					box-sizing: border-box;
					color: #4472c4;

					.cageId {
						font-size: 80rpx
					}

					.cage-numbers {
						display: flex;
						flex-direction: column;
						align-items: center;
						font-size: 20rpx;
						margin-top: 8rpx;
					}
				}

				.right {
					width: 100%;
					display: flex;
					flex-direction: row;
					justify-content: space-around;
					align-content: space-around;
					flex-wrap: wrap;

					.num1 {
						@include flex(column, center, 'items') width: 70%;
						height: 30%;
						font-size: 35rpx;
						font-weight: bold;
						border-radius: 8rpx;
						box-sizing: border-box;
						border: 1px solid #7c9bd2;
						justify-content: space-around;
						padding: 8rpx;
						margin: 0;
					}

					.num {
						@include flex(column, center, 'items') width: 42%;
						height: 30%;
						margin: 3rpx;
						font-size: 35rpx;
						font-weight: bold;
						border-radius: 8rpx;
						box-sizing: border-box;
						border: 1px solid #7c9bd2;
						justify-content: space-around;
					}
				}
			}
		}
	}
</style>